package com.lovers.mybatisplus;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Arrays;


/**
 * @Auther: wangzefeng
 * @Date: 2020/7/21 11:46
 * @Description:
 */
public class MyBatisPlusGenerator {

    //main函数
    public static void main(String[] args) {

        AutoGenerator autoGenerator = new AutoGenerator();

        //全局配置
        GlobalConfig gc = new GlobalConfig();
        String oPath = System.getProperty("user.dir");//得到当前项目的路径
        gc.setOutputDir(oPath + "/src/main/java");   //生成文件输出根目录
        gc.setOpen(false);//生成完成后不弹出文件框
        gc.setFileOverride(true);  //文件覆盖
        gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(true);// XML columList
        gc.setAuthor("wangzefeng");// 作者
        gc.setSwagger2(true);
        // 自定义文件命名，注意 %s 会自动填充表实体属性！
        gc.setControllerName("%sController");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        autoGenerator.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);   //设置数据库类型，我是postgresql
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        dsc.setUrl("jdbc:mysql://localhost:3306/manage?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false");  //指定数据库
        autoGenerator.setDataSource(dsc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);      // 表名生成策略
        strategy.setInclude(new String[] {
                "sys_config",
                "sys_department",
                "sys_file",
                "sys_login_log",
                "sys_menu",
                "sys_role",
                "sys_role_menu",
                "sys_user",
                "sys_user_role",
                "workflow_business",
                "workflow_form",
                "workflow_link",
                "workflow_node",
                "workflow_post",
                "workflow_post_user",
                "workflow_task",
                "workflow_type"
        });     // 需要生成的表
        strategy.setSuperServiceClass(null);
        strategy.setSuperServiceImplClass(null);
        strategy.setSuperMapperClass(null);
        autoGenerator.setStrategy(strategy);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.sys.manage");
        pc.setController("controller");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setMapper("mapper");
        pc.setEntity("domain");
        pc.setXml("xml");
        autoGenerator.setPackageInfo(pc);

        InjectionConfig injectionConfig=new InjectionConfig() {
            @Override
            public void initMap() {

            }
        };
        FileOutConfig fileOutConfig = new FileOutConfig() {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return tableInfo.getEntityName() + "Page" ;
            }
        };
        fileOutConfig.setTemplatePath("/templates/formPage.java.vm");
        injectionConfig.setFileOutConfigList(Arrays.asList(fileOutConfig));
        autoGenerator.setCfg(injectionConfig);
//
//        TemplateConfig template=new TemplateConfig();
//        template.setController("");
//        autoGenerator.setTemplate(template);
        // 执行生成
        autoGenerator.execute();
    }
}
